package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.activity.NewsDesActivity;
import com.winz.cultureapp.adapter.xrcv.ProjectAdapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.EventBusBean;
import com.winz.cultureapp.entity.ProjectListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.MarginDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 项目资源库
 */
public class ProjectFragment extends BaseFragment implements OnItemClickListener, XRecyclerView.LoadingListener {
    @BindView(R.id.xrcv_project)
    public XRecyclerView mXrcv_project;

    private boolean isRefresh = true;
    private int limit = 12;
    private int page = 1;
    private int totalPage = 0;

    private List<ProjectListBean.DataBean.ListBean> mDatas;
    private ProjectAdapter mAdapter;

    private String type = ""; // 筛选类型

    private String cid = "";

    public static ProjectFragment getInstance(String cid){

        ProjectFragment fragment = new ProjectFragment();

        Bundle bundle = new Bundle();
        bundle.putString("cid",cid);

        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_project;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        GridLayoutManager manager = new GridLayoutManager(mContext,3);
        mXrcv_project.addItemDecoration(new MarginDecoration(mXrcv_project,getResources().getDimensionPixelOffset(R.dimen.layout_padding_small),3));
        mXrcv_project.setLayoutManager(manager);
        mXrcv_project.setPullRefreshEnabled(true);
        mXrcv_project.setLoadingMoreEnabled(true);

        mAdapter = new ProjectAdapter(mContext,mDatas,R.layout.empty_view,R.layout.item_project);

    }

    @Override
    protected void initEvents() {

        mXrcv_project.setAdapter(mAdapter);
        mXrcv_project.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);

    }

    @Override
    protected void getDatas(Bundle arguments) {
        if (arguments != null) {
            cid = arguments.getString("cid");
            if (!TextUtils.isEmpty(cid)){
                getList();
            }
        }
    }



    /**
     * 获取数据列表
     */


    private void getList() {
        showProgressDialog();

        HttpUitls.onGet("legacy", RequestFactory.getInstance().legacy(page+"",limit+"",cid), new OnResponeListener<ProjectListBean>() {
            @Override
            public void onSuccess(ProjectListBean bean) {
                Logger.d("legacy==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                            totalPage = bean.totalPage;
                    if (bean.data.list != null && bean.data.list.size() > 0) {
                        mXrcv_project.setLoadingMoreEnabled(true);
                        if (isRefresh) {
                            mDatas.clear();
                            mDatas = bean.data.list;
                        } else {
                            mDatas.addAll(bean.data.list);
                        }
                    } else {
                        mDatas.clear();
                        ProjectListBean.DataBean.ListBean empty = new ProjectListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_project.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                } else {
                    showMsg(bean.msg);
                }
                if (isRefresh) {
                    mXrcv_project.refreshComplete();
                } else {
                    mXrcv_project.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                showMsg(error);
                if (isRefresh) {
                    mXrcv_project.refreshComplete();
                } else {
                    mXrcv_project.loadMoreComplete();
                }
            }
        });
    }


    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).legacyId);
        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,mDatas.get(position).title);
        bundle.putString(Constants.KEY.NEWS.HREF,Constants.URL.IP + "/legacy/detail/" + mDatas.get(position).legacyId);
        startActivity(NewsDesActivity.class,false,bundle);

    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page>=totalPage){
            mXrcv_project.setNoMore(true,true,"没有更多数据了");
        }else {
            page++;
            getList();
        }
    }

    @Override
    protected void onVisible() {
        super.onVisible();

        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onInvisible() {
        super.onInvisible();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void messageEventBus(EventBusBean<String> event){
        if (event.type == 0){
            showMsg(event.t);
        }

    }

}
